import java.util.ArrayList;
import java.util.Collections;

public class RankOfHands 
{
	public static ArrayList<PlayerServiceThread> compareSets(final ArrayList<PlayerServiceThread> Players)
	{
		int biggest=0;
		ArrayList<PlayerServiceThread> result = new ArrayList<PlayerServiceThread>();
		
		for(int i=0; i< Players.size(); i++)
		{
			if(numberOfCards(Players.get(i).player.actualHand).hand.size() > biggest)
			{
				biggest=numberOfCards(Players.get(i).player.actualHand).hand.size();
				result.clear();
				result.add(Players.get(i));
			}
			else if(numberOfCards(Players.get(i).player.actualHand).hand.size() == biggest)
			{
				result.add(Players.get(i));			
			}
		}
		return result;
	}
	
	public static Hand numberOfCards(final Hand hand)
	{
		for(int i=0;i<hand.hand.size() ;i++)
		{
			
			for(int j=i+1; j<hand.hand.size(); j++)
			{
				
				if((hand.hand.get(i).suit.equalsIgnoreCase(hand.hand.get(j).suit)))
				{
					
					if((hand.hand.get(i).rank.ordinal()) <= (hand.hand.get(j).rank.ordinal()))
					{
						Table.deck.cardsUsed.add(hand.hand.get(j));
						hand.hand.remove(j);
						numberOfCards(hand);
					}
					else
					{
						Table.deck.cardsUsed.add(hand.hand.get(i));
						hand.hand.remove(i);
						numberOfCards(hand);
					}		
				}
			}
		}
		for(int i=0;i<hand.hand.size() ;i++)
		{for(int j=i+1; j<hand.hand.size(); j++)
			{
				if((hand.hand.get(i).rank.ordinal()==hand.hand.get(j).rank.ordinal()))
				{
					
					if((hand.hand.get(i).rank.ordinal()) >= (hand.hand.get(j).rank.ordinal()))
					{
						Table.deck.cardsUsed.add(hand.hand.get(j));
						hand.hand.remove(j);
						numberOfCards(hand);
					}
					else
					{
						Table.deck.cardsUsed.add(hand.hand.get(i));
						hand.hand.remove(i);
						numberOfCards(hand);
					}
				}
			}
		}
		return hand;
	}
	public static PlayerServiceThread compare(final ArrayList<PlayerServiceThread> players)   
	{
		ArrayList<PlayerServiceThread> result=new ArrayList<PlayerServiceThread>();
		result= (compareSets(players));
		for(int i=0; i<result.size();  i++)
		{
			if(result.get(i).player.folded)
			{
				result.remove(i);
				i=0;
			}
		}
		if(result.size()==1) 
		{
			return result.get(0);
		}
		for(int i=0; i<result.size();  i++)
		{
			Collections.sort(result.get(i).player.actualHand.hand);
		}
		for(int i=0; i<result.size()-1;  i++)
		{
			if(result.size()==1) {
				return result.get(0);
			}
			if( compareTwoPlayers(result.get(i).player, result.get(i + 1).player) == 1 )
			{
				result.remove(i+1);
				i=-1;
			}
			else if( compareTwoPlayers(result.get(i).player, result.get(i + 1).player) == -1 )
			{
				result.remove(i);
				i=-1;
			}
		}	
		if(result.isEmpty())
		{
			return null;
		}
		else
		{
			return result.get(0);
		}
		
	}
	
	public static int compareTwoPlayers(Player player1,Player player2)
	{
		int size=0;
		size=player1.actualHand.hand.size();
		for(int i=size; i==0; i--)
		{
			if((player1.actualHand.hand.get(i).rank.ordinal()) < (player2.actualHand.hand.get(i).rank.ordinal()))
			{
				return 1;
			}
			else if((player1.actualHand.hand.get(i).rank.ordinal()) > (player2.actualHand.hand.get(i).rank.ordinal()))
			{
				return -1;
			}
		}
		return 0;
	}
}
